/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Conversations;

/**
 *
 * @author NDH
 */
public class ConverDAO extends AbstractDAO {

    /**
     * @param Conversations a conversation's info entered to create new one
     * @return int
     * @see a list
     */
    public int create(Conversations cs) {
        try {
            tx = session.beginTransaction();
            session.save(cs);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * @param Conversations a conversation for find out what sub-conversation is
     * the latest time
     * @return int
     */
    public int getMaxOrderNo(Conversations con) {
        query = session.createQuery("select max(mc.orderNumber) from MessageConversations  mc with "
                + "mc.conversations =:con");
        query.setParameter("con", con);
        try {
            return Integer.parseInt(query.list().get(0).toString());
        } catch (Exception e) {
            return -1;
        }

    }
//    public List<d> readReceivedMail(Accounts acc){
//    query = session.createQuery("from Conversations where")
//    }
//    public Conversations readBy(Logins login, Messages mes){
//        query  = session.createQuery("from Conversations c join c.messageConversations mc where mc.messages = :mesID "
//                 + " and mc.receiver = :loginID");
//         query.setParameter("mesID", mes);
//         query.setParameter("loginID", receiver);
//         try {
//            return (MessageConversation) query.list().get(0);
//         } catch (Exception e) {
//             return null;
//         }
//    }
}
